define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'editable', 'echarts', 'echarts-theme'], function ($, undefined, Backend, Table, Form, undefined, Echarts, undefined) {

    var Controller = {
        index: function () {
            // 初始化表格參數配置
            Table.api.init({
                extend: {
                    index_url: 'aps/statistics/index',
                    add_url: '',
                    edit_url: '',
                    del_url: '',
                    multi_url: 'aps/statistics/multi',
                    table: 'aps_statistics',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                showToggle: false,
                sortName: 'id',
                searchFormVisible: true,
                showToggle: false,
                showColumns: false,
                showExport: false,
                search: false,
                dblClickToEdit: false, //是否啟用雙擊編輯
                columns: [
                    [
                        //{checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        //{field: 'mix_id', title: __('Mix_id')},
                        //{field: 'workorder_id', title: __('Workorder_id')},
                        //{field: 'mainprocess_id', title: __('Mainprocess_id')},
                        {field: 'workorder.name', title: __('Workorder.name')},
                        {field: 'mainprocess.name', title: __('Mainprocess.name')},
                        {field: 'procedure.name', title: __('Procedure.name')},
                        //{field: 'procedure_id', title: __('Procedure_id')},
                        {field: 'procedure_manhours', title: __('平均工時'), operate:false},
                        //{field: 'procedure_manhours', title: __('Procedure_manhours'), operate:'BETWEEN'},
                        //{field: 'workorder_numbers', title: __('Workorder_numbers')},
                        //{field: 'procedure_man_id', title: __('Procedure_man_id')},
                        //{field: 'finished_datatime', title: __('Finished_datatime'), operate:'RANGE', addclass:'datetimerange'},
                        //{field: 'workorder.id', title: __('Workorder.id')},
                        //{field: 'workorder.order_id', title: __('Workorder.order_id')},
                        //{field: 'workorder.name', title: __('Workorder.name')},
                        //{field: 'workorder.number', title: __('Workorder.number')},
                        //{field: 'workorder.startdate', title: __('Workorder.startdate'), operate:'RANGE', addclass:'datetimerange'},
                        //{field: 'workorder.finishdate', title: __('Workorder.finishdate'), operate:'RANGE', addclass:'datetimerange'},
                        //{field: 'workorder.num', title: __('Workorder.num')},
                        //{field: 'workorder.mainprocess_id', title: __('Workorder.mainprocess_id')},
                        //{field: 'workorder.subprocess_id', title: __('Workorder.subprocess_id')},
                        //{field: 'workorder.procedure1_id', title: __('Workorder.procedure1_id')},
                        //{field: 'workorder.procedure2_id', title: __('Workorder.procedure2_id')},
                        //{field: 'workorder.procedure3_id', title: __('Workorder.procedure3_id')},
                        //{field: 'workorder.procedure4_id', title: __('Workorder.procedure4_id')},
                        //{field: 'workorder.procedure5_id', title: __('Workorder.procedure5_id')},
                        //{field: 'workorder.procedure6_id', title: __('Workorder.procedure6_id')},
                        //{field: 'workorder.procedure7_id', title: __('Workorder.procedure7_id')},
                        //{field: 'workorder.procedure8_id', title: __('Workorder.procedure8_id')},
                        //{field: 'workorder.procedure9_id', title: __('Workorder.procedure9_id')},
                        //{field: 'workorder.procedure10_id', title: __('Workorder.procedure10_id')},
                        //{field: 'workorder.procedure11_id', title: __('Workorder.procedure11_id')},
                        //{field: 'workorder.procedure12_id', title: __('Workorder.procedure12_id')},
                        //{field: 'workorder.procedure1_manhours', title: __('Workorder.procedure1_manhours'), operate:'BETWEEN'},
                        //{field: 'workorder.procedure2_manhours', title: __('Workorder.procedure2_manhours'), operate:'BETWEEN'},
                        //{field: 'workorder.procedure3_manhours', title: __('Workorder.procedure3_manhours'), operate:'BETWEEN'},
                        //{field: 'workorder.procedure4_manhours', title: __('Workorder.procedure4_manhours'), operate:'BETWEEN'},
                        //{field: 'workorder.procedure5_manhours', title: __('Workorder.procedure5_manhours'), operate:'BETWEEN'},
                        //{field: 'workorder.procedure6_manhours', title: __('Workorder.procedure6_manhours'), operate:'BETWEEN'},
                        //{field: 'workorder.procedure7_manhours', title: __('Workorder.procedure7_manhours'), operate:'BETWEEN'},
                        //{field: 'workorder.procedure8_manhours', title: __('Workorder.procedure8_manhours'), operate:'BETWEEN'},
                        //{field: 'workorder.procedure9_manhours', title: __('Workorder.procedure9_manhours'), operate:'BETWEEN'},
                        //{field: 'workorder.procedure10_manhours', title: __('Workorder.procedure10_manhours'), operate:'BETWEEN'},
                        //{field: 'workorder.procedure11_manhours', title: __('Workorder.procedure11_manhours'), operate:'BETWEEN'},
                        //{field: 'workorder.procedure12_manhours', title: __('Workorder.procedure12_manhours'), operate:'BETWEEN'},
                        //{field: 'workorder.update_time', title: __('Workorder.update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'workorder.create_time', title: __('Workorder.create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'workorder.ProductNumber', title: __('Workorder.productnumber')},
                        //{field: 'workorder.ProductName', title: __('Workorder.productname')},
                        //{field: 'workorder.MainProcessName', title: __('Workorder.mainprocessname')},
                        //{field: 'mainprocess.id', title: __('Mainprocess.id')},
                        //{field: 'mainprocess.name', title: __('Mainprocess.name')},
                        //{field: 'mainprocess.number', title: __('Mainprocess.number')},
                        //{field: 'mainprocess.manhours', title: __('Mainprocess.manhours'), operate:'BETWEEN'},
                        //{field: 'mainprocess.state', title: __('Mainprocess.state')},
                        //{field: 'mainprocess.create_time', title: __('Mainprocess.create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'mainprocess.update_time', title: __('Mainprocess.update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'procedure.id', title: __('Procedure.id')},
                        //{field: 'procedure.mainprocess_id', title: __('Procedure.mainprocess_id')},
                        //{field: 'procedure.devicetype_id', title: __('Procedure.devicetype_id')},
                        //{field: 'procedure.name', title: __('Procedure.name')},
                        //{field: 'procedure.number', title: __('Procedure.number')},
                        //{field: 'procedure.manhours', title: __('Procedure.manhours'), operate:'BETWEEN'},
                        //{field: 'procedure.state', title: __('Procedure.state')},
                        //{field: 'procedure.create_time', title: __('Procedure.create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'procedure.update_time', title: __('Procedure.update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 為表格綁定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        gstj: function () {

            var myChart = Echarts.init(document.getElementById('echart'), 'walden');

            var posList = [
                'left', 'right', 'top', 'bottom',
                'inside',
                'insideTop', 'insideLeft', 'insideRight', 'insideBottom',
                'insideTopLeft', 'insideTopRight', 'insideBottomLeft', 'insideBottomRight'
            ];

            var app = [];
            app.configParameters = {
                rotate: {
                    min: -90,
                    max: 90
                },
                align: {
                    options: {
                        left: 'left',
                        center: 'center',
                        right: 'right'
                    }
                },
                verticalAlign: {
                    options: {
                        top: 'top',
                        middle: 'middle',
                        bottom: 'bottom'
                    }
                },
                position: {
                    options: Echarts.util.reduce(posList, function (map, pos) {
                        map[pos] = pos;
                        return map;
                    }, {})
                },
                distance: {
                    min: 0,
                    max: 100
                }
            };

            app.config = {
                rotate: 90,
                align: 'left',
                verticalAlign: 'middle',
                position: 'insideBottom',
                distance: 10,
                onChange: function () {
                    var labelOption = {
                        normal: {
                            rotate: app.config.rotate,
                            align: app.config.align,
                            verticalAlign: app.config.verticalAlign,
                            position: app.config.position,
                            distance: app.config.distance
                        }
                    };
                    myChart.setOption({
                        series: [{
                            label: labelOption
                        }, {
                            label: labelOption
                        }, {
                            label: labelOption
                        }, {
                            label: labelOption
                        }]
                    });
                }
            };

            var labelOption = {
                normal: {
                    show: true,
                    position: app.config.position,
                    distance: app.config.distance,
                    align: app.config.align,
                    verticalAlign: app.config.verticalAlign,
                    rotate: app.config.rotate,
                    formatter: '{c}  {name|{a}}',
                    fontSize: 8,
                    rich: {
                        name: {
                            textBorderColor: '#fff'
                        }
                    }
                }
            };

            var xlist = [];
            var y1list = [];
            var y2list = [];
            for (var i = 0; i < Statsdata.list.length; i++) {
                //xlist.push('工單：' + Statsdata.list[i].workorder.name + '<br>規格：' + Statsdata.list[i].workorder.MainProcessName+ '<br>品號：' + Statsdata.list[i].workorder.ProductNumber+ '<br>品名：' + Statsdata.list[i].workorder.ProductName+ '<br>產量' + Statsdata.list[i].workorder.num);
                //xlist.push(Statsdata.list[i].workorder.name );
                xlist.push({
                    value: [
                        i,
                        Statsdata.list[i].workorder.name,
                        '規格：' + Statsdata.list[i].workorder.MainProcessName,
                        '品號：' + Statsdata.list[i].workorder.ProductNumber,
                        '品名：' + Statsdata.list[i].workorder.ProductName,
                        '產量：' + Statsdata.list[i].workorder.num
                    ]
                });
                y1list.push(Math.ceil(Statsdata.list[i].allhours * 60));
                y2list.push(Math.ceil(Statsdata.list[i].AVGManhours * 60));
            }

            option = {
                title: {
                    text: "",
                    subtext: ' 單位:分鐘',
                    left: 'left'
                },
                color: ['#003366', '#4cabce'],
                tooltip: {//提示
                    trigger: 'axis',
                    axisPointer: {
                        type: 'shadow'
                    },
                    formatter: function (params) {
                        console.log(params[0]);
                        return "工單：" + params[0].axisValue[1] + "<br>" + params[0].axisValue[2]+ "<br>" + params[0].axisValue[3]+ "<br>" + params[0].axisValue[4]+ "<br>" + params[0].axisValue[5];
                    }
                },
                legend: {
                    //data: ['Forest', 'Steppe', 'Desert', 'Wetland']
                    data: ['總工時', '平均']
                },
                // 右邊的工具欄
                //toolbox: {
                //    show: true,
                //    orient: 'vertical',
                //    left: 'right',
                //    top: 'center',
                //    feature: {
                //        mark: {show: true},
                //        dataView: {show: true, readOnly: false},
                //        magicType: {show: true, type: ['line', 'bar', 'stack', 'tiled']},
                //        restore: {show: true},
                //        saveAsImage: {show: true}
                //    }
                //},
                calculable: true,
                xAxis: [
                    {
                        type: 'category',
                        axisTick: {show: false},
                        data: xlist,
                        axisLabel: {
                            interval: 0,//橫軸信息全部顯示
                            formatter: function (val) {
                                return val[1];
                            }
                        },

                    }
                ],
                yAxis: [
                    {
                        type: 'value'
                    }
                ],
                series: [
                    {
                        name: '總工時',
                        type: 'bar',
                        barGap: 0,
                        label: labelOption,
                        data: y1list
                    },
                    {
                        name: '平均',
                        type: 'bar',
                        label: labelOption,
                        data: y2list
                    }
                ],
                dataZoom : [
                    {
                        type: 'slider',//移滑動條
                        show: false,
                        start: 0,
                        end: 50,
                    },
                    {
                        type: 'inside',//移圖
                        start: 0,
                        end: 50
                    }
                ]
            };

            myChart.setOption(option);

        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});